// pages/water/recharge/index.js
var app = getApp()
var common_Js = require("../../../component/common.js"), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
const CryptoJS = require("../../../utils/aesUtil.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card_id: 0,
    money:0,
    showMoney:0,
    singlePrice: 30,
    totalPrice:0,
    bucketCount:[],
    submitState: false,
    buyCount:0,
    myBucketCount:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.onHttpInit();
  },
  
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/bucket/recharge/view', {
    }, function (res) {
      if (res.data.code == 0) {
        console.log("水桶购买押金界面加载数据返回数据", res.data.data)
        that.data.singlePrice = res.data.data.singlePrice;
        that.setData({
          singlePrice: res.data.data.singlePrice,
          bucketCount: res.data.data.bucketCount,
          myBucketCount: res.data.data.myBucketCount,
        })
        that.setData({ 
          isLoading: false, loading: true, netWork: true, 
        })
      } else {
        console.log("水桶购买押金界面加载数据 异常", res.data.msg)
      }
    }, function () {
      that.setData({ isLoading: false, loading: true, netWork: true, })
    })
  },
  chooseMoney(e){
    console.log('chooseHour',e);
    var chooseIndex = e.currentTarget.dataset.index;
    for(var index in this.data.bucketCount){
      this.data.bucketCount[index].isCheck = 0;
    }
    this.data.bucketCount[chooseIndex].isCheck = 1;
    this.data.buyCount = this.data.bucketCount[chooseIndex].dictValue;
    console.log('buyCount', this.data.buyCount)
    this.data.money = this.data.bucketCount[chooseIndex].dictValue * this.data.singlePrice;
    console.log('this.data.money', this.data.money)
    var show = parseFloat(this.data.money+'')  * 100;
    console.log('this.data.show', show)
    this.setData({
      bucketCount: this.data.bucketCount,
      showMoney: show,
      totalPrice: this.data.bucketCount[chooseIndex].dictValue
    })
  },
  goOrder(){
    wx.navigateTo({
      url: '../order/index',
    })
  },
  submitOp:function(){
    if(this.data.buyCount == 0){
      Toast('请选择购买数量')
      return;
    }
    this.data.submitState = true;
    this.setData({submitState : true });
    var that = this;
    fetchApi.httpPost('/bucket/order/add', {
      count: that.data.buyCount,
    }, function (res) {
      console.log("水卡充值订单返回数据", res)
      if (res.data.code == 0) {
        var orderId = res.data.data;
        that.requestPay(orderId);
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  requestPay:function(order_id){
    var that = this;
    fetchApi.httpPost('/bucket/order/requestPay', {
      order_id: order_id,
      remark:that.data.remark,
      subject:'水桶押金'
    }, function (res) {
      console.log('提交订单',res)
      if (res.data.code == 0) {
        var pay_data = res.data.data;
        var wxpayinfo = JSON.parse(res.data.data)
        console.log('pay_data',pay_data)
        if(wxpayinfo.result_code == 'SUCCESS'){
          
          that.data.submitState = false;
          that.setData({submitState : false });
          wx.requestPayment({
            timeStamp: wxpayinfo.timeStamp,
            nonceStr: wxpayinfo.nonceStr,
            package: wxpayinfo.package,
            signType: wxpayinfo.signType,
            paySign: wxpayinfo.paySign,
            success (res) { 
              console.log('requestPayment success',res)
              wx.redirectTo({
                url: '../result/index',
              })
            },
            fail (res) {
              console.log('requestPayment fail',res)
             }
          })
        }
      } else {
        that.setData({ isLoading: false, loading: true, netWork: true })
      }
    }, function () {
        that.setData({  isLoading: false,  loading: true,  netWork: true, }) 
    })
  },
})